﻿#include "pch.h"
#include "read.h"

using namespace std;
any read::datachange(string out, vector<string> datatype, int datacol) {
	if (datatype[datacol] == "int") {
		return stoi(out);
	}
	if (datatype[datacol] == "long") {
		return stol(out);
	}
	if (datatype[datacol] == "float") {
		return stof(out);
	}
	if (datatype[datacol] == "double") {
		return stod(out);
	}
	if (datatype[datacol] == "string") {
		return out;
	}
	return 0;
}
vector<vector<any>> read::readf(string p_String,vector<vector<any>> &output, vector<string> &outputtype)
{
	fstream  f(p_String.c_str());
	vector<string> words;
	string line;
	string out;
	string isover;
	vector<vector<any>> datainput;
	vector<int> inputsize;
	vector<int> outputsize;
	vector<string> inputtype;
	getline(f, line);
	line = line.substr(0, 5);
	if (line == "input") {
		getline(f, line);
		stringstream input(line);
		while (input.good()) {
			getline(input, out, ' ');
			if (!out.empty())
				inputsize.push_back(atoi(out.c_str()));
		}
		getline(f, line);
		stringstream input2(line);
		while (input2.good()) {
			getline(input2, out, ' ');
			if (!out.empty())
				inputtype.push_back(out);
		}
		//for(int i=0;i<inputsize.size();i++){
		//	cout<<inputsize[i]<<endl;
		//}
	//	for(int i=0;i<inputtype.size();i++){
	//		cout<<inputtype[i]<<endl;
	//	}

		while (getline(f, line))
		{
			vector<any> tem;
			isover = line.substr(0, 6);
			if (isover == "output")break;
			words.push_back(line);
			stringstream input(line);
			int col = 0;
			while (input.good()) {
				getline(input, out, ' ');
				if (!out.empty()) {
					any anytem = read::datachange(out, inputtype, col);
					tem.push_back(anytem);
				}
				col++;
			}
			datainput.push_back(tem);
		}

	}
	getline(f, line);
	stringstream input(line);
	while (input.good()) {
		getline(input, out, ' ');
		if (!out.empty())
			outputsize.push_back(atoi(out.c_str()));
	}
	getline(f, line);
	stringstream input2(line);
	while (input2.good()) {
		getline(input2, out, ' ');
		if (!out.empty())
			outputtype.push_back(out);
	}
	//for(int i=0;i<outputsize.size();i++){
	//	cout<<outputsize[i]<<endl;
	//}
//	for(int i=0;i<outputtype.size();i++){
//		cout<<outputtype[i]<<endl;
//	}

	while (getline(f, line))
	{
		vector<any> tem;
		words.push_back(line);
		stringstream input(line);
		int col = 0;
		while (input.good()) {
			getline(input, out, ' ');
			if (!out.empty()) {
				any anytem = datachange(out, outputtype, col);
				tem.push_back(anytem);
			}
			col++;
		}
		output.push_back(tem);
	}
	return datainput;
}
